﻿using NPOI.SS.UserModel;
using System.Linq;

namespace Soul.XLS
{
    public static class IWorkbookExtensions
    {
        public static IFont CreeteDefaultFont(this IWorkbook workbook)
        {
            var font = workbook.CreateFont();
            font.FontName = "宋体";
            return font;
        }

        public static ICellStyle CreeteDefaultStyle(this IWorkbook workbook)
        {
            return workbook.CreateCellStyle();
        }

        internal static ExcelCellStyle CreeteExcelCellStyle(this IWorkbook workbook)
        {
            var font = workbook.CreeteDefaultFont();
            var style = workbook.CreeteDefaultStyle();
            style.SetFont(font);
            return new ExcelCellStyle(font, style);
        }

        internal static ExcelCellStyle CreeteExcelCellTitleStyle(this IWorkbook workbook)
        {
            var font = workbook.CreeteDefaultFont();
            font.IsBold = true;
            font.FontHeight = 400;
            var style = workbook.CreeteDefaultStyle();
            style.WrapText = true;
            style.WrapText = true;
            style.Alignment = HorizontalAlignment.Center;
            style.VerticalAlignment = VerticalAlignment.Center;
            style.SetFont(font);
            return new ExcelCellStyle(font, style);
        }

        public static ISheet AddTable(this IWorkbook workbook, ExcelTable table, int firstRow = 0, int firstColumn = 0)
        {
            var sheet = string.IsNullOrEmpty(table.Name) ?
                workbook.CreateSheet() : workbook.CreateSheet(table.Name);
            sheet.InsertTable(table, firstRow, firstColumn);
            return sheet;
        }
    }
}

